"""Symmetric array representation
Source: http://stackoverflow.com/questions/2572916/numpy-smart-symmetric-matrix
"""

import numpy as np

class SymNDArray(np.ndarray):
    """
    Symmentric array

    A change of element a[i,j] causes symmetric change of a[j,i]

    Usage
    -----
    tmp = numpy.array(...)
    sym_array = tmp.view(SymNDArray)
    """

    def __setitem__(self, (i, j), value):
        np.ndarray.__setitem__(self, (i, j), value)
        np.ndarray.__setitem__(self, (j, i), value)


def symarray(input_array):
    """
    Create symmetric array

    Usage
    -----
    tmp = numpy.array(...)
    sym_array = symarray(tmp)
    """
    return input_array.view(SymNDArray)
